package com.glink.manage.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.glink.common.exception.GeneralSimpleException;
import com.glink.common.util.LogUtil;
import com.glink.common.vo.ResponseResult;
import com.glink.manage.dto.participatingunit.*;
import com.glink.manage.service.ParticipatingUnitService;
import com.glink.manage.vo.participatingunit.ParticipatingUnitBaseVO;
import com.glink.manage.vo.participatingunit.ParticipatingUnitListVO;
import com.glink.manage.vo.participatingunit.ParticipatingUnitPageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author  qiushaoshan
 * @since  2025-01-08
 */
@RestController
@RequestMapping("/API/Manage/participatingUnit")
@Api(tags = "参建单位")
@Slf4j
public class ParticipatingUnitController extends AbstractBaseController {

    @Autowired
    private ParticipatingUnitService participatingUnitService;

    @ApiOperation(value = "01.新增参建单位")
    @PostMapping(value = "/insert", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Boolean> insertMeasurePoint(@Validated @RequestBody ParticipatingUnitSaveDTO dto) throws GeneralSimpleException {
        log.info("01.新增参建单位：{}", LogUtil.encode(dto));
        boolean status = participatingUnitService.saveParticipatingUnit(dto);
        log.info("01.新增参建单位：success");
        return this.response(status);
    }

    @ApiOperation(value = "02.更新参建单位")
    @PostMapping(value = "/update", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Boolean> updateMeasurePoint(@Validated @RequestBody ParticipatingUnitUpdateDTO dto) throws GeneralSimpleException {
        log.info("02.更新参建单位：{}", LogUtil.encode(dto));
        boolean status = participatingUnitService.updateParticipatingUnit(dto);
        log.info("02.更新参建单位：success");
        return this.response(status);
    }

    @ApiOperation(value = "03.根据ID查询参建单位详情")
    @PostMapping(value = "/findById", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<ParticipatingUnitBaseVO> findById(@Validated @RequestBody ParticipatingUnitIdDTO dto) throws GeneralSimpleException {
        log.info("04.根据ID查询参建单位详情：{}", LogUtil.encode(dto));
        ParticipatingUnitBaseVO baseVO = participatingUnitService.findById(dto);
        log.info("04.根据ID查询参建单位详情：success");
        return this.response(baseVO);
    }

    @ApiOperation(value = "05.分页查询参建单位")
    @PostMapping(value = "/findByPage", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<IPage<ParticipatingUnitPageVO>> findByPage(@Validated @RequestBody ParticipatingUnitPageDTO dto) throws GeneralSimpleException {
        log.info("05.分页查询巡视点位：{}", LogUtil.encode(dto));
        IPage<ParticipatingUnitPageVO> pageVOIPage = participatingUnitService.findByPage(dto);
        log.info("05.分页查询巡视点位：success");
        return this.response(pageVOIPage);
    }

    @ApiOperation(value = "06.导入参建单位")
    @PostMapping(value = "/import", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Integer> importParticipatingUnit(@Validated MultipartFile file) throws GeneralSimpleException {
        log.info("06.导入参建单位");
        Integer saveSuccessCount = participatingUnitService.importParticipatingUnit(file);
        log.info("06.导入参建单位：success");
        return this.response(saveSuccessCount);
    }

    @ApiOperation(value = "07.查询参建单位列表")
    @GetMapping(value = "/findList", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<List<ParticipatingUnitListVO>> findList() throws GeneralSimpleException {
        log.info("07.查询参建单位列表");
        List<ParticipatingUnitListVO> unitListVOList = participatingUnitService.findList();
        log.info("07.查询参建单位列表：success");
        return this.response(unitListVOList);
    }

    @ApiOperation(value = "08.加入/移除黑名单")
    @PostMapping(value = "/addBlacklist", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Boolean> addBlacklist(@Validated @RequestBody ParticipatingUnitBlackListIdDTO dto) throws GeneralSimpleException {
        log.info("08.加入/移除黑名单：{}", LogUtil.encode(dto));
        Boolean status = participatingUnitService.addBlacklist(dto);
        log.info("08.加入/移除黑名单：success");
        return this.response(status);
    }


    @ApiOperation(value = "09.根据ID删除参建单位")
    @PostMapping(value = "/delById", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Boolean> delById(@Validated @RequestBody ParticipatingUnitIdDTO dto) throws GeneralSimpleException {
        log.info("09.根据ID删除参建单位：{}", LogUtil.encode(dto));
        Boolean status = participatingUnitService.delById(dto);
        log.info("09.根据ID删除参建单位：success");
        return this.response(status);
    }
}
